package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.pojo.Page;
import com.itheima.admin.pojo.Setmeal;
import com.itheima.admin.pojo.SetmealDishes;
import com.itheima.admin.vo.SetmealVo;
import com.itheima.common.R;
import com.itheima.admin.mapper.SetmealDishMapper;
import com.itheima.admin.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import com.itheima.util.PageUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/18 10:04
 * @PackageName:com.itheima.service.impl
 * @ClassName: SetmealServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param
     * @return
     */
    @Override
    public R addDish(Setmeal setmeal) {
        boolean flag = setmealService.save(setmeal);
        List<SetmealDishes> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDishes setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setCreateTime(new Date());
            setmealDish.setCreateUser(setmeal.getCreateUser());
            setmealDishMapper.insert(setmealDish);
        }
//         保存套餐与菜品的关联信息
        if (flag) {
            return R.success("新增套餐成功");
        }
        return R.error("添加失败");
    }

    /**
     * 套餐分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R selectSetmeal(String name, int page, int pageSize) {
        int offset = PageUitl.getOffset(page, pageSize);
        List<Setmeal> setmeals = setmealMapper.selectSetmeal(name, offset, pageSize);
        int total = setmealMapper.selectCount(name);
        System.out.println("分页查询：" + setmeals);
        Page page1 = PageUitl.getPage(setmeals, total, page, pageSize);
        return R.success(page1);
    }

    /**
     * 修改套餐
     *
     * @param setmeal
     * @return
     */
    @Override
    public R updateSetmeal(Setmeal setmeal) {
        QueryWrapper<SetmealDishes> wrapper = new QueryWrapper<>();
        List<SetmealDishes> setmealDishes = setmeal.getSetmealDishes();
        int update = setmealMapper.updateById(setmeal);
//        删除对应套餐内菜品信息
        wrapper.eq("setmeal_id", setmeal.getId());
        int delete = setmealDishMapper.delete(wrapper);
        if (delete > 0) {
            System.out.println("菜品对应口味删除成功");
        }
//        添加套餐内新获取的菜品信息
        if (setmealDishes != null) {
            for (SetmealDishes setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setUpdateTime(new Date());
                setmealDish.setUpdateUser(setmeal.getCreateUser());
                setmealDishMapper.insert(setmealDish);
            }
        }
        if (update > 0) {
            return R.success("更新套餐成功");
        }
        return R.error("更新失败");
    }

    /**
     * 按id查询套餐信息
     *
     * @param id
     * @return
     */
    @Override
    public R selectSetmealById(String id) {
        QueryWrapper<SetmealDishes> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);
        List<SetmealDishes> setmealDishes = setmealDishMapper.selectList(wrapper);
        System.out.println("套餐菜品："+setmealDishes);
        if (setmealDishes == null) {
            return R.error("查询套餐内菜品时异常");
        }
        Setmeal setmeal = setmealMapper.selectById(id);
        setmeal.setSetmealDishes(setmealDishes);
        System.out.println("按id查询结果：" + setmeal);
        if (setmeal != null) {
            return R.success(setmeal);
        }
        return R.error("按id查询失败");
    }

    /**
     * 批量上架/下架
     * @param status
     * @param ids
     * @param updateUser
     * @return
     */
    @Override
    public R updateSetmealStatus(int status, List<String> ids, String updateUser) {
        int i = 0;
        for (String id : ids) {
            UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("id", id);

            updateWrapper.set("status", status);
            updateWrapper.set("update_time", new Date());
            updateWrapper.set("update_user", updateUser);
            setmealMapper.update(null, updateWrapper);
            i++;
        }
        if (i == ids.size()) {
            return R.success("修改状态成功"+i);
        } else {
            return R.error("修改状态失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public R deleteSetmeal(List<String> ids) {
        System.out.println("批量删除的id集合："+ids);
        int delete = setmealMapper.deleteBatchIds(ids);
        int i = 0;
//        删除菜品下的口味信息
        for (String id : ids) {
            int delete1 = setmealDishMapper.deleteSetmealDish(id);
            if (delete1>0){
                i++;
            }
        }
        if (ids.size()==i){
            System.out.println("菜品对应口味删除成功");
        }
        if (delete > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
}
